package be.kuleuven.cs.mop.gui.dialogs;


import java.awt.Frame;
import java.util.Set;

import be.kuleuven.cs.mop.app.controllers.Controllers;
import be.kuleuven.cs.mop.app.controllers.TaskController;
import be.kuleuven.cs.mop.domain.model.Task;



/**
 * {@link RemoveDialog} for the "Remove Task" use case
 */
public class RemoveTaskDialog extends RemoveDialog<Task> {
	private static final long serialVersionUID = 1L;
	
	
	public RemoveTaskDialog(final Frame parent, final Controllers controllers) {
		super(parent, "Remove Task", controllers);
	}
	
	
	
	@Override
	protected Task[] getItems() throws Exception {
		return getControllers().getControllerTasks().getUserTasks().toArray(new Task[0]);
	}
	
	@Override
	protected void remove(final Task item) throws Exception {
		final TaskController		controller	=
			getControllers().getControllerTasks();
		final Set<? extends Task>	dependants	= item.getDependants();
		
		if (dependants.isEmpty() || !new QuestionDialog(getOwner(),
				"Remove all depending tasks recursively?",
				dependants).getAnswer()) {
			controller.removeTask(item);
		} else {
			controller.removeTaskRecursively(item);
		}
	}
	
	@Override
	protected String toString(final Task item) {
		return String.format("Task (ID %d) of user %s",
				item.hashCode(),
				item.getUser().getName());
	}
	
}